package light.head.controller.sys;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.config.Config;
import light.head.config.ConfigParm;
import light.head.config.RunStatus;
import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.sys.HandleDAO;
import light.head.dao.sys.MessageTemplateDAO;
import light.head.dao.sys.OrganDAO;
import light.head.dao.sys.OrganTypeDAO;
import light.head.framework.base.SupporterController;
import light.head.framework.utils.XmlUtils;

import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = Url.C_CONFIG)
public class ConfigController extends SupporterController {
	
	private final static String config_path = "WEB-INF/classes/config/config.xml";
	
	
	private HandleDAO handleDAO;
	private OrganDAO organDAO;
	private OrganTypeDAO otDAO;
	private MessageTemplateDAO mtDAO;
	


	@RequestMapping(value = Sys.NONE)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setAttribute(Parm.PAGE_SIZE, ConfigParm.getPageSize());
		request.setAttribute(Parm.RECORD_LOG, ConfigParm.getRecordLog());
		request.setAttribute(Parm.CONTEXT_PATH, ConfigParm.getContextPath());
		request.setAttribute(Parm.PROJECT_PATH, ConfigParm.getProjectPath());
		request.setAttribute(Parm.CACHE_NOTICE_TOTAL, ConfigParm.getCacheNoticeTotal());
		
		return new ModelAndView(Url.CONFIG_EDIT);
	}
	
	@RequestMapping(value = Url.SAVE)
	public void save(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String pageSize = request.getParameter(Parm.PAGE_SIZE);
		String recordLog = request.getParameter(Parm.RECORD_LOG);
		String contextPath = request.getParameter(Parm.CONTEXT_PATH);
		String projectPath = request.getParameter(Parm.PROJECT_PATH);
		String cacheNoticeTotal = request.getParameter(Parm.CACHE_NOTICE_TOTAL);

		
		Element e_pageSize = XmlUtils.getElementByAttrId(Config.getConfigDoc().getRootElement(), Parm.PAGE_SIZE);
		Element e_recordLog = XmlUtils.getElementByAttrId(Config.getConfigDoc().getRootElement(), Parm.RECORD_LOG);
		Element e_contextPath = XmlUtils.getElementByAttrId(Config.getConfigDoc().getRootElement(), Parm.CONTEXT_PATH);
		Element e_projectPath = XmlUtils.getElementByAttrId(Config.getConfigDoc().getRootElement(), Parm.PROJECT_PATH);
		Element e_cacheNoticeTotal = XmlUtils.getElementByAttrId(Config.getConfigDoc().getRootElement(), Parm.CACHE_NOTICE_TOTAL);
	
		
		e_pageSize.attribute(Parm.VALUE).setData(pageSize);
		e_recordLog.attribute(Parm.VALUE).setData(recordLog);
		e_contextPath.attribute(Parm.VALUE).setData(contextPath);
		e_projectPath.attribute(Parm.VALUE).setData(projectPath);
		e_cacheNoticeTotal.attribute(Parm.VALUE).setData(cacheNoticeTotal);
		
		
		
		String savePath = projectPath + config_path;
		XmlUtils.saveXML(savePath, Config.getConfigDoc());
		Config.loadConfig();
		ConfigParm.loadConfigParm();
		
		print(response, Msg.SUCCESS);
	}
	
	@RequestMapping(value = Url.CACHE)
	public void cache(HttpServletRequest request, HttpServletResponse response) {
		handleDAO.loadAll();
		organDAO.loadAll();
		otDAO.loadAll();
		mtDAO.loadAll();
		print(response, Msg.SUCCESS);
	}

	@RequestMapping(value = Url.LOAD_INDEX_CACHE)
	public void loadIndexCache(HttpServletRequest request, HttpServletResponse response) throws Exception {
		RunStatus.pcIndexCreateState = 0;
		print(response, Msg.SUCCESS);
	}
	
	

	
	@Autowired
	public void setHandleDAO(HandleDAO handleDAO) {
		this.handleDAO = handleDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	@Autowired
	public void setOtDAO(OrganTypeDAO otDAO) {
		this.otDAO = otDAO;
	}
	@Autowired
	public void setMtDAO(MessageTemplateDAO mtDAO) {
		this.mtDAO = mtDAO;
	}
}
